//
//  ContactBookViewController.m
//  GolfFriend
//
//  Created by vescky on 13-12-16.
//  Copyright (c) 2013年 vescky.org. All rights reserved.
//

#import "ContactBookViewController.h"
#import "SearchFriendsViewController.h"
#import "ContactBook.h"
#import "OneToOneChatViewController.h"
#import "SVProgressHUD.h"
#import "SearchCoreManager.h"

@interface ContactBookViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate> {
    NSMutableArray *friendList,*searchResultList,*dataSource;
}

@end

@implementation ContactBookViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"名媛录"];
    [super customBackButton];
    [self initView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private
- (void)initView {
    searchResultList = [[NSMutableArray alloc] init];
    dataSource = [[NSMutableArray alloc] init];
    
    friendList = [[ContactBook shareContactBook] getFriendList];
    dataSource = friendList;
    
    if (!friendList || [friendList count] < 1) {
        [tbFriendList setHidden:YES];
        CGRect rectnv = viewNoData.frame;
        rectnv.origin.y = 50;
        viewNoData.frame = rectnv;
        [self.view addSubview:viewNoData];
        return;
    }
    
    NSArray *phoneArr = [[NSArray alloc] initWithObjects:@"1",@"2", nil];
    for (int i = 0; i < [friendList count]; i++) {
        NSDictionary *fItem = [friendList objectAtIndex:i];
        [[SearchCoreManager share] AddContact:[fItem objectForKey:@"recordId"] name:[fItem objectForKey:@"nickName"] phone:phoneArr];
    }
    
}

- (void)searchFriend:(NSString*)keyword {
    if (keyword.length < 1) {
//        [SVProgressHUD showErrorWithStatus:@"搜索内容不能为空!"];
        return;
    }
    keyword = [keyword lowercaseString];
    [searchResultList removeAllObjects];
    NSMutableArray *arr1 = [[NSMutableArray alloc] init],*arr2 = [[NSMutableArray alloc] init];
    NSMutableArray *arrList = [[NSMutableArray alloc] init];
    
    [[SearchCoreManager share] Search:keyword searchArray:nil nameMatch:arr1 phoneMatch:arr2];
    if (arr1) {
        [arrList addObjectsFromArray:arr1];
    }
    if (arr2) {
        [arrList addObjectsFromArray:arr2];
    }
    
    for (int i = 0; i < [arrList count]; i++) {
        int rId = [[arrList objectAtIndex:i] integerValue];
        for (int j = 0; j < [friendList count]; j++) {
            int crId = [[[friendList objectAtIndex:j] objectForKey:@"recordId"] integerValue];
            if (crId == rId) {
                [searchResultList addObject:[friendList objectAtIndex:j]];
            }
        }
    }
    dataSource = searchResultList;
    [tbFriendList reloadData];
}

#pragma mark - Public
- (IBAction)btnAction:(id)sender {
    UIButton *btn = (UIButton*)sender;
    if (btn.tag == 200) {
//
        
        [tfSearch resignFirstResponder];
    }
    else if (btn.tag == 201) {
        //添加好友
//        [self searchFriend];
        SearchFriendsViewController *vc = [[SearchFriendsViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (btn.tag == 202) {
        tfSearch.text = @"";
        dataSource = friendList;
        [tbFriendList reloadData];
        viewDoneEditing.hidden = YES;
    }
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.inputAccessoryView = viewDoneEditing;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    textField.inputAccessoryView = nil;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *currentString = [NSString stringWithFormat:@"%@%@",textField.text,string];
    if (currentString.length > 0 && string.length > 0) {
        viewDoneEditing.hidden = NO;
        [self searchFriend:currentString];
    }
    else {
        dataSource = friendList;
        [tbFriendList reloadData];
    }
    
    return YES;
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"ContactBookCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(!cell){
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ContactBookCell" owner:self options:nil] objectAtIndex:0];
        //        cell = [[ChatListViewCell alloc] init];
    }
    
    if ([cell respondsToSelector:@selector(setDataInfo:)]) {
        [cell performSelector:@selector(setDataInfo:) withObject:[dataSource objectAtIndex:indexPath.row]];
    }
    
    [cell setTag:200+indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tbFriendList deselectRowAtIndexPath:indexPath animated:YES];
    OneToOneChatViewController *ochatVc = [[OneToOneChatViewController alloc] init];
    ochatVc.userId = [[dataSource objectAtIndex:indexPath.row] objectForKey:@"uId"];
    [self.navigationController pushViewController:ochatVc animated:YES];
}

@end
